package com.younge.spring.component.security;

import com.younge.spring.repository.dao.SysUser;
import com.younge.spring.repository.SysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserRepository sysUserRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 获取用户基本信息
        Optional<SysUser> sysUserOpt = sysUserRepository.findByUsername(username);
        sysUserOpt.orElseThrow(() -> new UsernameNotFoundException("用户不存在"));
        sysUserOpt.ifPresent(sysUser -> {
            Long userId = sysUser.getId();
        });
//        // 用户停用
//        if (StatusEnums.DISABLE.getKey().equals(user.getStatus())) {
//            throw new ApiException(SysExceptionEnum.USER_DISABLED);
//        }

//        // 获取角色
//        Set<String> roles = sysUserRoleMapper.listRoleKeyByUserId(userId);
//
//        // 获取数据范围标识
//        Integer dataScope = sysUserRoleMapper.getMaximumDataScope(roles);
//
//        Set<String> permissions = new HashSet<>();
//        // 如果 roles 包含 root 则拥有所有权限
//        if (roles.contains(CommonConstant.SUPER_ADMIN_ROOT)) {
//            permissions.add(CommonConstant.ALL_PERMISSION);
//        } else {
//            // 获取菜单权限标识
//            permissions = sysMenuMapper.getMenuPermission(userId);
//            // 过滤空字符串
//            permissions.remove("");
//        }

        //return new SysUserDetails(sysUser, permissions, roles, username, dataScope);
        return new SysUserDetails();
    }
}
